package com.sfx.concurrent.demo.concurrent.pattern.GuardedObjectEncapsulation;

import java.util.Hashtable;
import java.util.Map;

class MailBoxes{
    private static Map<Integer,GuardedObject> mailbox = new Hashtable<>();
    private static int id;

    //生成唯一的id
    public  synchronized int generateId(){
        return id++;
    }

    //生成GuardObject-->利用线程安全的集合
    public GuardedObject createGuardedObject(){
        GuardedObject go = new GuardedObject(generateId());
        mailbox.put(go.getId(),go);
        return go;
    }
    //通过id得到GuardedObject
    public GuardedObject GetGuardedObject(int id){
        return mailbox.remove(id);
    }
}